package com.koron.css2.meterService.impl;

import com.koron.css2.baseConfig.mapper.MeterBoreMapper;
import com.koron.css2.baseConfig.mapper.MeterFactoryMapper;
import com.koron.css2.baseConfig.queryBean.MeterBoreQueryBean;
import com.koron.css2.baseConfig.queryBean.MeterFactoryQueryBean;
import com.koron.css2.baseConfig.vo.MeterBoreVO;
import com.koron.css2.baseConfig.vo.MeterFactoryVO;
import com.koron.css2.common.dto.AttachmentDto;
import com.koron.css2.common.service.AbstractExportExcel;
import com.koron.css2.meterService.bean.query.MsInfoQueryBean;
import com.koron.css2.meterService.dto.MsInfoDto;
import com.koron.css2.meterService.mapper.MsInfoMapper;
import com.koron.css2.serviceManage.bean.BaseBusinessBranchBean;
import com.koron.css2.serviceManage.bean.BaseMeterModelBean;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.mapper.BaseBusinessBranchMapper;
import com.koron.css2.serviceManage.mapper.BaseMeterModelMapper;
import com.koron.css2.serviceManage.queryBean.BaseBusinessBranchQueryBean;
import com.koron.css2.serviceManage.queryBean.BaseMeterModelQueryBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Tools;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

/**
 * 水表明细导出
 * @author 周利斌
 *
 */
public class MsInfoExport extends AbstractExportExcel {

	@Override
	public AttachmentDto getAttachment(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		AttachmentDto attachment = new AttachmentDto();
		MsInfoQueryBean bean = JsonUtils.objectToPojo(req.getData(), MsInfoQueryBean.class);
		MsInfoMapper mapper = factory.getMapper(MsInfoMapper.class);		
		List<MsInfoDto> list = mapper.selectMsInfoList(bean);	
		list.stream().forEach(t -> {setMsInfo(factory, t);});
		attachment.setBtlTemplate("ms_info.btl");
		attachment.setFileTemplate("ms_info.xlsx");
		attachment.setData(list);
		attachment.setFileType("excel");
		attachment.setFilename("水表明细");
		return attachment;
	}

	
	public MsInfoDto setMsInfo(SessionFactory factory, MsInfoDto t) {
		Map<String, String> meterStatus = Tools.mapDicByCode(factory, "MMS");
		Map<String, String> meterForms = Tools.mapDicByCode(factory, "MMF");
		Map<String, String> meterTypes = Tools.mapDicByCode(factory, "MMT");
		Map<String, String> commOperators = Tools.mapDicByCode(factory, "MMF");
		MeterFactoryMapper meterFactoryMapper = factory.getMapper(MeterFactoryMapper.class,"_default");
		BaseMeterModelMapper meterModelMapper = factory.getMapper(BaseMeterModelMapper.class,"_default");
		BaseBusinessBranchMapper businessBranchMapper = factory.getMapper(BaseBusinessBranchMapper.class);
		MeterBoreMapper meterBoreMapper = factory.getMapper(MeterBoreMapper.class,"_default");
		List<MeterFactoryVO> meterFactorys = meterFactoryMapper.selectMeterFactoryList(new MeterFactoryQueryBean());
		List<BaseMeterModelBean> baseMeterModels = meterModelMapper.selectMeterModelList(new BaseMeterModelQueryBean());
		List<MeterBoreVO> meterBores = meterBoreMapper.selectMeterBoreList(new MeterBoreQueryBean());
		List<BaseBusinessBranchBean> businessBranchs = businessBranchMapper.selectBusinessBranchList(new BaseBusinessBranchQueryBean());
			// 水表口径
		if (StringUtils.isNotBlank(t.getMeterBore())) {
			MeterBoreVO meterBore = meterBores.stream().filter(k -> Objects.equals(t.getMeterBore(), k.getId())).findFirst().orElse(new MeterBoreVO());
			t.setMeterBoreName(meterBore.getBoreName());
		}
		if (StringUtils.isNotBlank(t.getMeterStatus())) {
			// 水表状态
			t.setMeterStatusName(meterStatus.get(t.getMeterStatus()));
		}
		if (StringUtils.isNotBlank(t.getMeterForm())) {
			// 水表形态
			t.setMeterFormName(meterForms.get(t.getMeterForm()));
		}
		if (StringUtils.isNotBlank(t.getMeterType())) {
			// 水表类型
			t.setMeterTypeName(meterTypes.get(t.getMeterType()));
		}
		if (StringUtils.isNotBlank(t.getCommOperator())) {
			// 通讯商
			t.setCommOperator(commOperators.get(t.getCommOperator()));
		}
		if (StringUtils.isNotBlank(t.getMeterFactory())) {
			// 水表厂家
			Optional<MeterFactoryVO> optional = meterFactorys.stream().filter(o -> Objects.equals(o.getId(), t.getMeterFactory())).findFirst();
			t.setMeterFactoryName(optional.orElse(new MeterFactoryVO()).getFactoryName());
		}
		if (StringUtils.isNotBlank(t.getMeterModel())) {
			// 水表形态
			Optional<BaseMeterModelBean> optional = baseMeterModels.stream().filter(o -> Objects.equals(o.getId(), t.getMeterModel())).findFirst();
			t.setMeterModelName(optional.orElse(new BaseMeterModelBean()).getModelName());
		}
		if (StringUtils.isNotBlank(t.getDepartBelong())) {
			// 营业网点
			Optional<BaseBusinessBranchBean> optional = businessBranchs.stream().filter(o -> Objects.equals(o.getId(), t.getDepartBelong())).findFirst();
			t.setDepartBelongName(optional.orElse(new BaseBusinessBranchBean()).getBranchName());
		}
		return t;
	}

}
